package com.shmilyou.dissertationmanagement.web;
/* Created with 岂止是一丝涟漪     530060499@qq.com    2019/2/9 */

import com.shmilyou.dissertationmanagement.biz.SoftwareCopyrightService;
import com.shmilyou.dissertationmanagement.domain.SoftwareCopyright;
import com.shmilyou.dissertationmanagement.tools.Utils;
import com.shmilyou.dissertationmanagement.utils.Constant;
import com.shmilyou.dissertationmanagement.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Controller
public class SoftwareCopyrightController {

    private final SoftwareCopyrightService softwareCopyrightService;

    @Autowired
    public SoftwareCopyrightController(SoftwareCopyrightService softwareCopyrightService) {
        this.softwareCopyrightService = softwareCopyrightService;
    }

    @RequestMapping("/softwareCopyright")
    public String index(Integer pageIndex, Map<Object, Object> map) {
        if (pageIndex == null) {
            pageIndex = 1;
        }
        Page<SoftwareCopyright> page = softwareCopyrightService.queryAll(pageIndex - 1, 20);
        map.put("entities", page.getContent());
        map.put("page", page);
        return "softwareCopyright";
    }

    @RequestMapping(value = "/search/softwareCopyright", method = RequestMethod.GET)
    public String search(String name, ModelMap map) {
        if (StringUtils.isEmpty(name)) {
            return "redirect:/softwareCopyright";
        }
        List<SoftwareCopyright> copyrights = softwareCopyrightService.queryByName(name);
        map.put("entities", copyrights);
        return "softwareCopyright";
    }

    @RequestMapping(value = "/softwareCopyright/rm", method = RequestMethod.POST)
    public ResponseEntity<Map<String, Object>> delete(String id) {
        if (Utils.isInteger(id)) {
            SoftwareCopyright exit = softwareCopyrightService.queryById(Integer.parseInt(id));
            if (exit != null) {
                softwareCopyrightService.delete(exit);
                return WebUtils.success();
            }
        }
        return WebUtils.failure();
    }

    @RequestMapping(value = "/softwareCopyright/edit", method = RequestMethod.GET)
    public String edit(String id, ModelMap map) {
        if (Utils.isInteger(id)) {
            SoftwareCopyright entity = softwareCopyrightService.queryById(Integer.parseInt(id));
            map.put("entity", entity);
            return "softwareCopyright_edit";
        }
        return "error";
    }

    @RequestMapping(value = "/softwareCopyright/add", method = RequestMethod.GET)
    public String add() {
        return "softwareCopyright_add";
    }

    @RequestMapping(value = "/softwareCopyright/edit", method = RequestMethod.POST)
    public ResponseEntity<Map<String, Object>> editHandle(SoftwareCopyright entity, @RequestParam MultipartFile file) {
        String attachment = WebUtils.saveUploadFile(file, Constant.SOFTWARECOPYRIGHT_ATTACHMENTS_PATH);
        if (!StringUtils.isEmpty(attachment)) {
            entity.setAttachment(attachment);
        }
        softwareCopyrightService.update(entity);
        return WebUtils.success();
    }

    @RequestMapping(value = "/softwareCopyright/add", method = RequestMethod.POST)
    public ResponseEntity<Map<String, Object>> addHandle(SoftwareCopyright entity, @RequestParam MultipartFile file) {
        String attachment = WebUtils.saveUploadFile(file, Constant.SOFTWARECOPYRIGHT_ATTACHMENTS_PATH);
        if (!StringUtils.isEmpty(attachment)) {
            entity.setAttachment(attachment);
        }
        softwareCopyrightService.save(entity);
        return WebUtils.success();
    }

    @RequestMapping(value = "/softwareCopyright/export", method = RequestMethod.POST)
    public ResponseEntity<Map<String, Object>> exportSoftwareCopyright() {
        List<SoftwareCopyright> entities=softwareCopyrightService.queryAll();
        return WebUtils.success(entities);
    }
}
